package com.techwells.applicationMarket.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;
import com.techwells.applicationMarket.domain.Currency;
import com.techwells.applicationMarket.service.CurrencyService;
import com.techwells.applicationMarket.util.PagingTool;
import com.techwells.applicationMarket.util.ResultInfo;
import com.techwells.applicationMarket.util.UploadFileUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 *  币种的controller
 * @author 陈加兵
 */
@Api(description="币种的api")   //标注说明改接口的作用
@RestController
@RequestMapping("*.do")    //配置访问的后缀，只有后缀为.do的url才能访问到接口
public class CurrencyController {
	
	@Resource
	private CurrencyService currencyService;
	
	private Logger logger=LoggerFactory.getLogger(CurrencyController.class); //日志
	
	/**
	 * 添加 币种
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("/currency/addCurrency")
	public Object addCurrency(HttpServletRequest request,@RequestParam(value="logo",required=false)MultipartFile logo) throws Exception{
		ResultInfo resultInfo=new ResultInfo();
		String name=request.getParameter("name"); //币种名称
		String currencyName=request.getParameter("currencyName"); //currency
		String type=request.getParameter("type"); //1 墨客 2 井通
		String issuer=request.getParameter("issuer"); //发起人 井通用到
		String contractAddress=request.getParameter("contractAddress"); //智能合约地址 墨客
		
		
		if (StringUtils.isEmpty(name)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("币种名称不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(type)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("类型不能为空");
			return resultInfo;
		}
		
		if (logo==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("图标不能为空");
			return resultInfo;
		}
		
		//封装数据
		Currency currency = new Currency();
		currency.setName(name);
		currency.setType(Integer.parseInt(type));
		currency.setCreateDate(new Date());
		
		
		if (type.equals("2")) {  //如果是井通
			if (StringUtils.isEmpty(currencyName)) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("currency不能为空");
				return resultInfo;
			}
			
			if (StringUtils.isEmpty(issuer)) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("issuer不能为空");
				return resultInfo;
			}
			
			currency.setCurrencyName(currencyName);
			currency.setIssuer(issuer);
			
		}else {  //如果是墨客
			if (StringUtils.isEmpty(contractAddress)) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("智能合约地址不能为空");
				return resultInfo;
			}
			currency.setContractAddress(contractAddress);
		}
		
		//上传logo
		String fileName=System.currentTimeMillis()+logo.getOriginalFilename();
		String url = UploadFileUtils.uploadFile("currency-image", fileName, logo);
		currency.setLogo(url);
		
		
		
		//调用service层的方法
		try {
			Object object=currencyService.addCurrency(currency);
			return object;
		} catch (Exception e) {
			logger.error("添加币种异常",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("添加失败");
			return resultInfo;
		}
	}
	
	
	
	/**
	 * 获取 币种详情
	 * @param request
	 * @return
	 */
	@PostMapping("/currency/getCurrencyById")
	public Object getCurrencyById(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		String currencyId=request.getParameter("currencyId");
		
		if (StringUtils.isEmpty(currencyId)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage(" 币种Id不能为空");
			return resultInfo;
		}
		
		//调用service层的方法
		try {
			Object object=currencyService.getCurrencyById(Integer.parseInt(currencyId));
			return object;
		} catch (Exception e) {
			logger.error("获取 币种详细信息失败",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("获取失败");
			return resultInfo;
		}
	}
	
	
	/**
	 * 修改 币种
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("/currency/modifyCurrency")
	public Object modifyCurrency(HttpServletRequest request,@RequestParam(value="logo",required=false)MultipartFile logo) throws Exception{
		ResultInfo resultInfo=new ResultInfo();
		String currencyId=request.getParameter("currencyId");
		String name=request.getParameter("name"); //币种名称
		String currencyName=request.getParameter("currencyName"); //currency
		String type=request.getParameter("type"); //1 墨客 2 井通
		String issuer=request.getParameter("issuer"); //发起人 井通用到
		String contractAddress=request.getParameter("contractAddress"); //智能合约地址 墨客
		
		if (StringUtils.isEmpty(currencyId)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage(" 币种Id不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(name)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("币种名称不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(type)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("类型不能为空");
			return resultInfo;
		}
		
		
		//封装数据
		Currency currency = new Currency();
		currency.setId(Integer.parseInt(currencyId));
		currency.setName(name);
		currency.setType(Integer.parseInt(type));
		currency.setCreateDate(new Date());
		
		
		if (type.equals("2")) {  //如果是井通
			if (StringUtils.isEmpty(currencyName)) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("currency不能为空");
				return resultInfo;
			}
			
			if (StringUtils.isEmpty(issuer)) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("issuer不能为空");
				return resultInfo;
			}
			
			currency.setCurrencyName(currencyName);
			currency.setIssuer(issuer);
			
		}else {  //如果是墨客
			if (StringUtils.isEmpty(contractAddress)) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("智能合约地址不能为空");
				return resultInfo;
			}
			currency.setContractAddress(contractAddress);
		}
		
		if (logo!=null) {
			//上传logo
			String fileName=System.currentTimeMillis()+logo.getOriginalFilename();
			String url = UploadFileUtils.uploadFile("currency-image", fileName, logo);
			currency.setLogo(url);
		}
		
		//调用service层的方法
		try {
			Object object=currencyService.modifyCurrencyReturnObject(currency);
			return object;
		} catch (Exception e) {
			logger.error("修改 币种异常",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("修改失败");
			return resultInfo;
		}
	}
	
	
	/**
	 * 根据Id删除 币种
	 * @param request
	 * @return
	 */
	@PostMapping("/currency/deleteCurrencyById")
	public Object deleteCurrency(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		String currencyId=request.getParameter("currencyId");
		
		if (StringUtils.isEmpty(currencyId)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage(" 币种Id不能为空");
			return resultInfo;
		}
		
		//调用service层的方法
		try {
			Object object=currencyService.deleteCurrencyReturnObject(Integer.parseInt(currencyId));
			return object;
		} catch (Exception e) {
			logger.error("删除 币种异常",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("删除失败");
			return resultInfo;
		}
	}
	
	
	/**
	 * 分页获取 币种列表
	 * @param request
	 * @return
	 */
	@PostMapping("/currency/getCurrencyList")
	public Object getCurrencyList(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		
		String pageNum=request.getParameter("pageNum");
		String pageSize=request.getParameter("pageSize");
		
		String type=request.getParameter("type"); //类型 1 墨客  2 井通
		
		
		//校验数据
		if (StringUtils.isEmpty(pageNum)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("当前页数不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(pageSize)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("每页显示的数量不能为空");
			return resultInfo;
		}
		
		//构造分页数据
		PagingTool pagingTool=new PagingTool(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
		
		//封装过滤条件
		Map<String, Object> params=new HashMap<String, Object>();
		
		if (!StringUtils.isEmpty(type)) {
			params.put("type", Integer.parseInt(type));
		}
		
		pagingTool.setParams(params);
		
		//调用service方法
		try {
			Object object=currencyService.getCurrencyList(pagingTool);
			return object;
		} catch (Exception e) {
			logger.error("获取 币种列表异常",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("获取失败");
			return resultInfo;
		}
	}
}
